package com.topfun.dao;

import com.topfun.utils.JDBCUtil;
import com.topfun.vo.Goods;
import com.topfun.vo.GoodsChangeRecord;
import com.topfun.vo.GoodsExtend;

import java.util.List;

/**
 * Created by huangda on 2017/8/23.
 * 商品dao
 */
public class GoodsDao {
    // ===========================================================
    // Constants
    // ===========================================================
    private static final String SQL_QUERY_GOODS_LIST;
    private static final String SQL_QUERY_GOODS_BY_ID;
    private static final String SQL_QUERY_GOODS_EXTEND_BY_IDS;
    private static final String SQL_QUERY_GOODS_EXTEND_BY_GOODSID;
    private static final String SQL_UPDATE_GOODS_EXTEND_STATUS;
    private static final String SQL_INSERT_GOODS_CHANGE_RECORD;
    private static final String SQL_QUERY_GOODS_CHANGE_RECORD;
    private static final String SQL_QUERY_GOODS_CHANGE_RECORD_BY_USERID;
    private static final String SQL_UPDATE_GOODS_INVENTORY;
    static {
        SQL_QUERY_GOODS_LIST = "select goodsId,name,description,image,type,inventory,exchange,price,changePrice,status,createTime from topfun.goods where status = 1";
        SQL_QUERY_GOODS_BY_ID = "select goodsId,name,description,image,type,inventory,exchange,price,changePrice,status,createTime from topfun.goods where goodsId = ?";
        SQL_QUERY_GOODS_EXTEND_BY_IDS = "select seqId,goodsId,cardNumber,`key`,`status`,createTime from topfun.goods_extend where seqId in (%s) ";
        SQL_QUERY_GOODS_EXTEND_BY_GOODSID = "select seqId,goodsId,cardNumber,`key`,`status`,createTime from topfun.goods_extend where goodsId = ? and status = 0 limit ?";
        SQL_UPDATE_GOODS_EXTEND_STATUS = "update topfun.goods_extend  set status = ? where seqId in (%s)";
        SQL_INSERT_GOODS_CHANGE_RECORD = "insert into topfun.change_record (userId,goodsId,goodsExtendIds,userAddressId,createTime) values (?,?,?,?,?)";
        SQL_QUERY_GOODS_CHANGE_RECORD = "select seqId,userId,goodsId,goodsExtendIds,userAddressId,createTime from topfun.change_record where userId = ? and goodsExtendIds = '%s'";
        SQL_QUERY_GOODS_CHANGE_RECORD_BY_USERID = "SELECT " +
                "a.seqId,a.userId,a.goodsId,b.name,b.description,b.image," +
                "b.price,b.changePrice,a.goodsExtendIds,a.userAddressId,a.createTime " +
                "from topfun.change_record a " +
                "LEFT JOIN topfun.goods b ON a.goodsId = b.goodsId " +
                "where a.userId = ? order by seqId desc";
        SQL_UPDATE_GOODS_INVENTORY = "update topfun.goods set inventory = inventory - ? ,exchange = exchange + ? where goodsId = ?";
    }
    // ===========================================================
    // Fields
    // ===========================================================

    // ===========================================================
    // Constructors
    // ===========================================================

    // ===========================================================
    // Getter &amp; Setter
    // ===========================================================

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    // ===========================================================
    // Methods
    // ===========================================================
    public static List<Goods> queryGoodsList() {
        return JDBCUtil.queryList(Goods.class,SQL_QUERY_GOODS_LIST,null);
    }

    public static Goods queryGoodsById(long goodsId) {
        return JDBCUtil.queryForOne(Goods.class,SQL_QUERY_GOODS_BY_ID,new Object[]{goodsId});
    }

    public static List<GoodsExtend> queryGoodsExtendByGoodsId(long goodsId, int limit) {
        return JDBCUtil.queryList(GoodsExtend.class,SQL_QUERY_GOODS_EXTEND_BY_GOODSID,new Object[]{goodsId,limit});
    }

    public static int updateGoodsExtendStatus(String seqIds, int status) {
        return JDBCUtil.update(String.format(SQL_UPDATE_GOODS_EXTEND_STATUS,seqIds),new Object[]{status});
    }

    public static void insertGoodsChangeRecord(GoodsChangeRecord changeRecord) {
        JDBCUtil.insertForPrimaryKey(SQL_INSERT_GOODS_CHANGE_RECORD,new Object[]{
                //userId,goodsId,goodsExtendIds,
                changeRecord.userId,changeRecord.goodsId,changeRecord.goodsExtendIds,
                //userAddressId,createTime
                changeRecord.userAddressId,changeRecord.createTime
        });
    }

    public static List<GoodsExtend> queryGoodsExtendByIds(String goodsExtendIds) {
        return JDBCUtil.queryList(GoodsExtend.class,String.format(SQL_QUERY_GOODS_EXTEND_BY_IDS,goodsExtendIds),null);
    }

    public static GoodsChangeRecord queryChangeRecord(long userId, String goodsExtendIds) {
        return JDBCUtil.queryForOne(GoodsChangeRecord.class,String.format(SQL_QUERY_GOODS_CHANGE_RECORD,goodsExtendIds),new Object[]{userId});
    }
    public static List<GoodsChangeRecord> queryChangeRecordByUserId(long userId) {
        return JDBCUtil.queryList(GoodsChangeRecord.class,SQL_QUERY_GOODS_CHANGE_RECORD_BY_USERID,new Object[]{userId});
    }

    public static int updateInventory(long goodsId, int number) {
        return JDBCUtil.update(SQL_UPDATE_GOODS_INVENTORY,new Object[]{number,number,goodsId});
    }
    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}
